package com.podium.web.log.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.podium.comment.AjaxResult;
import com.podium.comment.annotate.OperateLog;
import com.podium.comment.annotate.PermAuth;
import com.podium.comment.constant.OperateLogType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.podium.web.log.entity.SysLoginLog;
import com.podium.web.log.service.SysLoginLogService;

import java.util.List;

/**
 * @author 邵文泽
 * @description 登录日志
 * @date 2025-05-13 08:46:11
 */
@RestController
@RequestMapping("/log/login/log")
public class SysLoginLogController {

    @Autowired
    private SysLoginLogService sysLoginLogService;

    /**
     * 分页获取登录日志信息
     *
     * @param sysLoginLog 查询登录日志信息条件
     */
    @PermAuth("log:login_log:list")
    @GetMapping("page")
    public AjaxResult<IPage<SysLoginLog>> getPage(@RequestParam(defaultValue = "1") int pageNum,
                                                  @RequestParam(defaultValue = "10") int pageSize,
                                                  SysLoginLog sysLoginLog
    ) {
        Page<SysLoginLog> page = new Page<>(pageNum, pageSize);
        return new AjaxResult<IPage<SysLoginLog>>().success(sysLoginLogService.page(page, sysLoginLog));
    }

    /**
     * 获取登录日志信息
     *
     * @param sysLoginLog 查询登录日志信息条件
     */
    @PermAuth("log:login_log:list")
    @GetMapping("list")
    public AjaxResult<List<SysLoginLog>> getList(SysLoginLog sysLoginLog) {
        return new AjaxResult<List<SysLoginLog>>().success(sysLoginLogService.list(sysLoginLog));
    }

    /**
     * 根据ID获取登录日志信息
     */
    @GetMapping("{id}")
    public AjaxResult<SysLoginLog> getByInfo(@PathVariable Long id) {
        return new AjaxResult<SysLoginLog>().success(sysLoginLogService.getById(id));
    }

    /**
     * 添加登录日志信息
     *
     * @param sysLoginLog 登录日志信息
     */
    @OperateLog(title = "登录日志", businessType = OperateLogType.ADD)
    @PermAuth("log:login_log:add")
    @PostMapping("/save")
    public AjaxResult<Void> save(@RequestBody SysLoginLog sysLoginLog) {
        boolean row = sysLoginLogService.save(sysLoginLog);
        return row ? new AjaxResult<Void>().success() : new AjaxResult<Void>().error();
    }

    /**
     * 更新登录日志信息
     *
     * @param sysLoginLog 登录日志信息
     */
    @OperateLog(title = "登录日志", businessType = OperateLogType.EDIT)
    @PermAuth("log:login_log:edit")
    @PostMapping("/update")
    public AjaxResult<Void> update(@RequestBody SysLoginLog sysLoginLog) {
        boolean row = sysLoginLogService.updateById(sysLoginLog);
        return row ? new AjaxResult<Void>().success() : new AjaxResult<Void>().error();
    }

    /**
     * 删除登录日志信息
     *
     * @param ids 登录日志信息ID集合
     */
    @OperateLog(title = "登录日志", businessType = OperateLogType.DELETE)
    @PermAuth("log:login_log:delete")
    @GetMapping("/remove/{ids}")
    public AjaxResult<Void> remove(@PathVariable Long[] ids) {
        if (ids == null || ids.length == 0) {
            return new AjaxResult<Void>().error("没有要删除的数据！");
        }
        for (Long id : ids) {
            sysLoginLogService.removeById(id);
        }
        return new AjaxResult<Void>().success();
    }

}